

package server;

/**
 *
 * @author Muhammad Asad Ullah
 */


public class protocolParser {
 


    /**
     *
     * @param request - as String send by the Client.
     * @return - Operation in this particular request.
     */
   public Operation getOperation(String request)
   {
       if(!request.contains("%%") || request.length() == 0)
           return Operation.INVALIDOPERATION;
       String [] ar = request.split("%%");
        if(ar.length < 2)
           return Operation.INVALIDOPERATION;
       String [] operation = ar[1].split("=");
       String s = operation[1];
       if(s.compareToIgnoreCase("login") == 0)
           return Operation.LOGIN;
       else if(s.compareToIgnoreCase("logout") == 0)
           return Operation.LOGOUT;
       else if(s.compareToIgnoreCase("sendmessage") == 0)
           return Operation.SENDMESSAGE;
       else if(s.compareToIgnoreCase("getuserlist") == 0)
           return Operation.GETUSERLIST;
       else if(s.compareToIgnoreCase("adduser") == 0)
           return Operation.ADDUSER;
       else if(s.compareToIgnoreCase("edit") == 0)
           return Operation.EDITUSER;
       else if(s.compareToIgnoreCase("deleteuser")== 0)
           return Operation.DELETEUSER;
       else if(s.compareToIgnoreCase("kick") == 0)
           return Operation.KICKUSER;
       else
           return Operation.INVALIDOPERATION;
   }
   /**
    *
    * @param op - Operation in this particular request
    * @param request -Request send by Client as String.
    * @return Parameters as String Array by parsing the request for provided Operation op.
    */
   public String [] getOperationParameters(Operation op,String request)
   {
       String [] parameters;
       String [] temp = request.split("%%");
       if(op == Operation.LOGIN)
       {
           String [] t = temp[4].split("=");           
           parameters = new String[3];
           parameters[0] = temp[2];
           parameters[1] = temp[3];
           parameters[2] = t[1];
           return parameters;
       }
       else if(op == Operation.LOGOUT)
       {
        parameters = new String[1];
        parameters[0] = temp[2];
        return parameters;
       }
       else if(op == Operation.SENDMESSAGE)
       {
           parameters = new String[2];
           parameters[0] = temp[2];
           parameters[1] = temp[3];
           return parameters;
       }
       else if(op == Operation.GETUSERLIST)
       {
           return null;
       }
       else if(op == Operation.ADDUSER)
       {
           String [] tmp = temp[4].split("=");
           parameters = new String[3];
           parameters[0] = temp[2];
           parameters[1] = temp[3];
           parameters[2] = tmp[1];
           return parameters;
       }
       else if(op == Operation.EDITUSER)
       {
           parameters = new String[4];
           parameters[0] = temp[2];
           parameters[1] = temp[3];
           parameters[2] = temp[4];
           parameters[3] = (temp[5].split("="))[1];
           return parameters;
       }
       else if(op == Operation.DELETEUSER)
       {
           parameters = new String[1];
           parameters[0] = temp[2];
           return parameters;
       }
       else if(op == Operation.KICKUSER)
       {
           parameters = new String[1];
           parameters[0] = temp[2];
           return parameters;
       }
       else  return null;
   }


  

}

